$(function () {
    var pay_datagrid = $("#pay_datagrid")
    var pay_dialog = $("#pay_dialog")
    var pay_form = $("#pay_form")
    var product_datagrid = $("#product_datagrid")
    pay_dialog.dialog({
        closed: true,
        title: '缴费处理',
        width: 400,
        height: 600,
        buttons: '#dialog_buttons'
    })
    product_datagrid.datagrid({
        title:'已购产品列表',
        fitColumns:true,
        // fit:true,
        columns: [[
                {field: 'productName', title: '险种',width:100},
                {field: 'annualFee', title: '险种金额',width:100},
            ]
        ]
    })
    pay_datagrid.datagrid({
        url: "/temporary/queryPay.do",
        fitColumns: true, // 自适应列
        fit: true,
        striped: true,  // 斑马线
        pagination: true,
        pageSize: 10,
        pageNumber: 1,
        singleSelect: true, // 单选
        toolbar: "#pay_toolbar",
        columns: [
            [
                {field: 'id', hidden: true},
                {
                    field: 'client', title: '投保人', width: 80, formatter: function (value, row) {
                        if (row.client) {
                            return row.client.name
                        }
                    }
                },
                {field: 'sn', title: '核保单号', width: 80},
                {field: 'totalAmount', title: '需缴金额', width: 80},
                {
                    field: 'time', title: '保险年限/年', width: 80
                },
                {field: 'auditDate', title: '核保日期', width: 80},
                {
                    field: 'organization', title: '保险机构', width: 80, formatter: function (value, row) {
                        if (row.organization) {
                            return row.organization.name
                        }
                    }
                }
            ]
        ]
    })
    var objMethod = {
        query: function () {
            //获取高级查询的参数值
            var clientName = $("#clientName").textbox('getValue')
            var sn = $("#sn").textbox('getValue')
            //重新加载数据表格
            pay_datagrid.datagrid('load', {
                clientName: clientName,
                sn: sn,
            });
        },
        pay: function () {
            var row = pay_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            //回显数据
            row["client.name"] = row.client.name
            row["organization.name"] = row.organization.name
            //回显数据表格
            $.get('/product/selectAllProduct.do', {insureId: row.id}, function (data) {
                product_datagrid.datagrid('loadData',data)
            })
            pay_form.form('load', row)
            //打开对话框
            pay_dialog.dialog('open')
        },
        refresh:function () {
            //刷新数据表格
            pay_datagrid.datagrid('reload')
        },
        submit:function () {
            pay_form.form('submit',{
                url:'/temporary/pay.do',
                success:function (data) {
                    console.log(data);
                    data = $.parseJSON(data)
                    console.log(data);
                    if (data.success) {
                        $.messager.alert('友情提示', '缴费成功','info',function () {
                            //关闭对话框
                            pay_dialog.dialog('close')
                            //刷新数据表格
                            pay_datagrid.datagrid('reload')
                        });
                    } else {
                        $.messager.alert("友情提示", data.msg, "warning")
                    }
                }
            })
        },
        cancel:function () {
            pay_dialog.dialog('close')
        },
    }
    // 绑定事件
    $('a[data-cmd]').click(function () {
        objMethod[$(this).data('cmd')]();
    })
})